
#include	<iostream>

#include	"zig/exception.hpp"

using namespace zig;

void	baz(void)
{
	THROW(0, "baz msg");
}

void	bar(void)
{
	TRY("bar msg", baz());
}

void	foo(void)
{
	TRY("foo msg", bar());
}

int	main(void)
{
	std::cerr << "step 11111\n";
	try {
		baz();
	}
	catch(Exception& ex) {
		std::cerr << ex << std::endl;
	}
	catch(...) {
		std::cerr << "unknown exception.";
	}

	std::cerr << "step 22222\n";
	try {
        try { 
			baz();
		} 
        catch (Exception &e) { 
            THROW_EX(e.code(), e, "step--22"); 
        } 
        catch (std::bad_alloc &e) { 
            THROW(EXTERNAL, "bad alloc " "step-22"); 
        } 
        catch (std::exception &e) { 
            THROW_FMT(EXTERNAL, "External exception " "step-22" ": %s",  e.what()); 
        } 
	}
	catch(Exception& ex) {
		std::cerr << "step 22222 catch Exception.\n";
		std::cerr << ex << std::endl;
	}
	catch(...) {
		std::cerr << "step 22222 catch something.\n";
		std::cerr << "unknown exception.";
	}



	std::cerr << "step 33333\n";
	try {
		TRY("main msg", baz());
	}
	catch(Exception& ex) {
		std::cerr << "step 33333 catch Exception.\n";
		std::cerr << ex << std::endl;
	}
	catch(...) {
		std::cerr << "step 33333 catch something.\n";
		std::cerr << "unknown exception.";
	}

	std::cerr << "step 33333322222\n";
	try {
        try { 
			bar();
		} 
        catch (Exception &e) { 
            THROW_EX(e.code(), e, "step--33333322"); 
        } 
        catch (std::bad_alloc &e) { 
            THROW(EXTERNAL, "bad alloc " "step-33333322"); 
        } 
        catch (std::exception &e) { 
            THROW_FMT(EXTERNAL, "External exception " "step-33333322" ": %s",  e.what()); 
        } 
	}
	catch(Exception& ex) {
		std::cerr << "step 33333322222 catch Exception.\n";
		std::cerr << ex << std::endl;
	}
	catch(...) {
		std::cerr << "step 33333322222 catch something.\n";
		std::cerr << "unknown exception.";
	}


	std::cerr << "step 44444\n";
	try {
		TRY("main msg", bar());
	}
	catch(Exception& ex) {
		std::cerr << "step 44444 catch Exception.\n";
		std::cerr << ex << std::endl;
	}
	catch(...) {
		std::cerr << "step 44444 catch something.\n";
		std::cerr << "unknown exception.";
	}

	std::cerr << "step 55555\n";
	try {
		TRY("main msg", foo());
	}
	catch(Exception& ex) {
		std::cerr << ex << std::endl;
	}

	return	0;
}

